import axios from 'axios';
import qs from 'qs' // axios自带的工具不需要安装npm依赖
import router from '../router/index';
import store from '../store/index';
import ElementUi from 'element-ui'
import { getToken } from '@/utils/auth'
function request(params, create, data) {
    let createAxios = {
       // baseURL: "http://192.168.0.108:8000/",
      // baseURL: "http://192.168.0.99:8000/",
      //baseURL: "http://192.168.0.99:8000/",
        baseURL: "http://82.156.188.202:80/",

        timeout: 5000,
        withCredentials: true,
        transformRequest: [function(data) {
            data = qs.stringify(data);
            return data;
        }]
    }


    createAxios = create || createAxios;
    const request = axios.create({
        ...createAxios
    });

    //封装token
    request.interceptors.request.use(
        config => {
            let token = getToken();
            if (token) {
                config.headers['token'] = token;
            }
            return config
        },
        error => {
            // do something with request error
            console.log(error) // for debug
            return Promise.reject(error)
        }
    )

    //响应拦截
    request.interceptors.response.use(response => {
      if (response.data.result == '3') {
          ElementUi.Message({
              showClose: true,
              message: `${response.data.explain}`,
              type: 'error',
              duration: 1800
          });
          router.push("/my");
      }
      if (response.data.result == "0") {
          ElementUi.Message({
              showClose: true,
              message: `${response.data.explain}`,
              type: 'error',
              duration: 1800
          });
      }
        if (response.data.result == "error") {
            ElementUi.Message({
                showClose: true,
                message: '登录已过期，请重新登录！',
                type: 'error',
                duration: 1800
            });
            router.push("/login");
        }
        if (response.data.result == "2") {
            ElementUi.Message({
                showClose: true,
                message: '登陆失效，请重新登陆！',
                type: 'error',
                duration: 1800
            });
            router.push("/login");
        }
        return response;
    }, err => {
        if (err.message == "Network Error" || err.message.includes("timeout")) {
            ElementUi.Message({
                showClose: true,
                message: '请求出错，当前暂时无法访问服务器！',
                type: 'error',
                duration: 1800
            })
        } else {
            ElementUi.Message({
                showClose: true,
                message: '请求出错：' + err.message,
                type: 'error',
                duration: 1800
            })
        }
        return err;
    })
    let requestD = {
        url: params.url,
        method: params.method || 'GET',
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        }
    };
    if (params.method === 'GET') {
        requestD['params'] = params.data;
    } else {
        requestD['data'] = params.data;
    }

    return new
    request(requestD).then(res => [res.message ? res : null, res.message ? null : res]);
}
export default {
    request,
    axios,
}
